package utils;

/**
 * 位运算实现RBAC权限管理
 * 通过‘&’、‘\’、'~'
 */
public class RbacUtils {

    /**
     * 权限判断
     * @param role
     * @param module
     * @return
     */
    public static boolean isAuth(int role, int module) {
        boolean result = false;
        if ((role & module) > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 增加权限
     * @param role
     * @param module
     * @return
     */
    public static int addAuth(int role, int module) {
        role = role | module;
        return role;
    }

    /**
     * 删除权限
     * @param role
     * @param module
     * @return
     */
    public static int removeAuth(int role, int module) {
        role = role & (~module);
        return role;
    }

    public static void main(String [] args) {
        int role = 0b0000;
        role = addAuth(role, 0b0001);
        System.out.println(role);
        if (isAuth(role, 0b0010)) {
            System.out.println("含有该权限");
        }
    }
}
